package com.itany.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.ResponseCodeConstant;
import com.itany.cms.dao.CommentMapper;
import com.itany.cms.entity.Comment;
import com.itany.cms.entity.Course;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.service.CommentService;
import com.itany.cms.service.CourseService;
import com.itany.cms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @RequestMapping("/findComment")
    public ModelAndView findComment(Comment courseParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Comment> commentPage = commentService.findByParams(courseParam,pageNo, pageSize);
        mav.addObject("commentPage", commentPage);
        mav.setViewName("backend/back_commentManageSet");
        return mav;
    }
    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            commentService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @RequestMapping("/findComment2")
    public ModelAndView findComment2(Comment courseParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Comment> commentPage = commentService.findByParams(courseParam,pageNo, pageSize);
        mav.addObject("commentPage", commentPage);
        mav.setViewName("backend/back_commentSet");
        return mav;
    }
    @ResponseBody
    @RequestMapping("/modifyStatus2")
    public ResponseResult modifyStatus2(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            commentService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
